<?php

class Admin_ClasssubjectController extends Zend_Controller_Action
{
	protected $_flashMessenger = null;
    public function init()
    {
    	$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			if ($auth->getIdentity ()->role != 'administrator') {
				$this->_helper->redirector ( 'index', 'auth', 'default' );
			}
		} else {
			$this->_helper->redirector ( 'index', 'auth', 'default' );
		}
		$this->_helper->layout->setLayout ( 'admin' );
		$this->_flashMessenger = $this->_helper->getHelper ( 'FlashMessenger' );
		$this->initView ();
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	
    	$request = $this->getRequest();
    	$class_id= $request->getParam('classid');
    	$classModel = new Application_Model_ClassModel();
    	
    	if(!$class_id) {$class_id = 1;}
    	
    	$class_subject = new Application_Model_Classsubject();
    	$subsellist = $class_subject->fetchAll($class_id);
    	
    	$subList= array();
    	foreach($subsellist as $list)
    	{
    		$subList[]=$list['subject_id'];
    	}
    	
    	print_r($subList);
    	$this->view->class_id = $class_id;
    	
    	$this->view->classlist = $classModel->fetchAll();
    	
    	$subjectsModel = new Application_Model_Subjects();
    	
    	$subjects_list = $subjectsModel->fetchall();
    	
    	$new_sub_list = array();
    	foreach($subjects_list as $list)
    	{
    		$new_sub_list[] = array('name' => $list['name'],
    								'id'	=> $list['id'],
    								'selected' => (in_array($list['id'], $subList)) ? true : false,
    		);
    	}
    	$this->view->list = $new_sub_list;
        // action body
    }

    public function saveAction()
    {
    	
    	$request = $this->getRequest();
    	
    	if($request->isPost())
    	{
    		$class_subject_model = new Application_Model_Classsubject();
    		$class_id =$request->getParam('classid');
    		$subjects_selected = $request->getParam('subselected');
    		foreach($subjects_selected as $key => $value)
    		{
    		$insert['class_id'] = $class_id;
    		$insert['subject_id'] =$key;
    		$class_subject_model->save($insert);
    		} 
    		$this->_flashMessenger->addMessage('Record Saved!');
			$this->_redirect('/admin/classsubject/index/classid/'.$class_id);
    	}
    	
        // action body
    }


}



